package com.lechi.iot.netty.handle;

import com.lechi.iot.netty.message.TcpMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 自定义编码器，将数据编码后进行下发
 */
public class TcpMessageEncoder extends MessageToByteEncoder<TcpMessage> {

    @Override
    public void encode(ChannelHandlerContext ctx, TcpMessage msg, ByteBuf out) {
        try {
            out.writeBytes(msg.toBytes());
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
